﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaunchMeNot.Core;
using LaunchMeNot.DAL;
using LaunchMeNot.Properties;
using Lime49.UI;

namespace LaunchMeNot
{
    public static class WPFConfigurationManager
    {
        public static double AutoLaunchDelay { get; set; }
        public static bool AutoLaunchAlwaysOnTop { get; set; }
        public static bool CloseAfterAutoLaunch { get; set; }
        public static string CountdownFormat { get; set; }
        //public static string FilePath { get; set; }

        /// <summary>
        /// Configures with values from application settings.
        /// </summary>
        internal static void ConfigureDefault() {
            Configure(new WPFConfiguration());
        }

        internal static void Configure(IWPFConfiguration config) {
            AutoLaunchDelay = config.AutoLaunchDelay;
            AutoLaunchAlwaysOnTop = config.AutoLaunchAlwaysOnTop;
            CountdownFormat = config.CountdownFormat;
            CloseAfterAutoLaunch = config.CloseAfterAutoLaunch;

            CoreConfigurationManager.Configure(config);
            DALConfigurationManager.Configure(config);
        }

        /// <summary>
        /// Ensures a database is available at the configured FilePath, creates one using the default path and provider otherwise.
        /// </summary>
        internal static void EnsureDatabaseValid()
        {
            if (string.IsNullOrEmpty(DALConfigurationManager.LaunchMeNotStore.FilePath)) {
                try {
                    string launchMeNotPath = System.IO.Path.Combine(Lime49.Utils.GetApplicationPath(), "launchmenot.xml");
                    Settings.Default.FilePath = launchMeNotPath;
                    DALConfigurationManager.LaunchMeNotStore.FilePath = launchMeNotPath;
                    Settings.Default.Save();
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    DialogBox.Show("Error setting path to LaunchMeNot entry database, configure it manually from Options", "Error", DialogBoxType.OK, DialogBoxIcon.Error, DialogBoxButton.OK);
                    return;
                }
            }
        }
    }

    public interface IWPFConfiguration : IDALConfiguration, ICoreConfiguration
    {
        double AutoLaunchDelay { get; set; }
        bool AutoLaunchAlwaysOnTop { get; set; }
        bool CloseAfterAutoLaunch { get; set; }
        string CountdownFormat { get; set; }
    }
}
